/*
 * Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <sys/time.h>
#include <unistd.h>

#include <libubox/ustream.h>

#include "libubus.h"

static struct ubus_context *ctx;
static struct blob_buf b;

static void test_client_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
{
	fprintf(stderr, "Subscribers active: %d\n", obj->has_subscribers);
}

static struct ubus_object test_client_object = {
	.subscribe_cb = test_client_subscribe_cb,
};

static const struct blobmsg_policy return_policy[] = {
	[0] = { .name = "deviceMode", .type = BLOBMSG_TYPE_STRING },
    [1] = { .name = "wirelessMode", .type = BLOBMSG_TYPE_STRING },
    [2] = { .name = "channelWidth", .type = BLOBMSG_TYPE_INT32 },
    [3] = { .name = "antennaGain", .type = BLOBMSG_TYPE_INT32 },
    [4] = { .name = "pointDistance", .type = BLOBMSG_TYPE_INT32 },
};

static void test_count_data_cb(struct ubus_request *req, int type, struct blob_attr *msg)
{
	struct blob_attr *tb[5];
	// int rc;
	// uint32_t count_to = *(uint32_t *)req->priv;

	blobmsg_parse(return_policy, 5, tb, blob_data(msg), blob_len(msg));

	if (tb[0]) {
		printf("deviceMode:%s\n", blobmsg_get_string(tb[0]));
	}
    if (tb[1]) {
		printf("wirelessMode:%s\n", blobmsg_get_string(tb[1]));
	}
    if (tb[2]) {
		printf("channelWidth:%d\n", blobmsg_get_u32(tb[2]));
	}
    if (tb[3]) {
		printf("antennaGain:%d\n", blobmsg_get_u32(tb[3]));
	}
    if (tb[4]) {
		printf("pointDistance:%d\n", blobmsg_get_u32(tb[4]));
	}
}

static void client_main(void)
{
	static struct ubus_request req;
	uint32_t id;
	int ret;

	ret = ubus_add_object(ctx, &test_client_object);
	if (ret) {
		fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));
		return;
	}

	if (ubus_lookup_id(ctx, "sihan.bridge", &id)) {
		fprintf(stderr, "Failed to look up test object\n");
		return;
	}
	blob_buf_init(&b, 0);
    blobmsg_add_string(&b, "ip", "192.168.2.101");
	ubus_invoke(ctx, id, "get_bridge_info", b.head, test_count_data_cb, 0, 3000);
	uloop_run_timeout(300);
}

int bridge_info(void)
{
	uloop_init();

	ctx = ubus_connect(NULL);
	if (!ctx) {
		fprintf(stderr, "Failed to connect to ubus\n");
		return -1;
	}

	ubus_add_uloop(ctx);

	client_main();

	ubus_free(ctx);
	uloop_done();

	return 0;
}

static void energy_time_sync_main(void)
{
	static struct ubus_request req;
	uint32_t id;
	int ret;

	ret = ubus_add_object(ctx, &test_client_object);
	if (ret) {
		fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));
		return;
	}

	if (ubus_lookup_id(ctx, "sihan.energy", &id)) {
		fprintf(stderr, "Failed to look up test object\n");
		return;
	}
	blob_buf_init(&b, 0);
    // blobmsg_add_string(&b, "ip", "192.168.2.101");
	ubus_invoke(ctx, id, "sync_timer", b.head, NULL, 0, 3000);
	uloop_run_timeout(300);
}
int energy_time_sync(void) {
    uloop_init();

	ctx = ubus_connect(NULL);
	if (!ctx) {
		fprintf(stderr, "Failed to connect to ubus\n");
		return -1;
	}

	ubus_add_uloop(ctx);

	energy_time_sync_main();

	ubus_free(ctx);
	uloop_done();
}
